

<template>
    <el-form :model="form">
        <el-form-item label="登录账号" :label-width="formLabelWidth">
            <el-input disabled v-model="form.adminname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
            <el-input type="password" v-model="form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="角色" :label-width="formLabelWidth">
            <el-select v-model.number="form.role" placeholder="请选择角色">
                <el-option v-for="item in roles" v-bind:label="item.label" v-bind:value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth">
            <el-tree ref="treeRef" :data="data" show-checkbox node-key="id" />
        </el-form-item>
        <el-form-item>
            <el-button @click="editOperFn">修改</el-button>
            <el-button @click="cancelFn">取消</el-button>
        </el-form-item>
    </el-form>
</template>
<script lang="ts" setup>
import { ref, reactive, watch } from "vue";
import type { IOper } from "@/MyTs";
import { roles, grantsData as data } from "./data";
import { getOperDataAPI, editOperatorApi } from "@/api/Login" //接口数据 获，修
import { ElTree, ElMessage } from "element-plus";

const formLabelWidth = "140px";
interface IProps {
    editadminname: string
}
const props = defineProps<IProps>();


interface IEmits {
    (e: "onOK"): void
    (e: "onCleas"): void
} //传递参数，没有返回值 
//子传父，在父组件的子标签上定义自定义事件，在子组件内使用emits触发事件。
const emits = defineEmits<IEmits>();//定义变量包含IEmits类型的对象用于组件事件
const form = reactive<IOper>({
    adminname: '',
    password: '',
    role: 0,
    checkedKeys: [],
    adminid: ''
})
const treeRef = ref<InstanceType<typeof ElTree>>();
//获取管理员数据
const addlistone = () => {
    console.log("123");

    getOperDataAPI(props.editadminname).then(res => {
        if (res.data.code == 200) {
            form.adminname = res.data.data[0].adminname
            form.password = res.data.data[0].password
            form.role = res.data.data[0].role
            form.checkedKeys = res.data.data[0].checkedKeys;
            // const checkedKeys = filterNodeone(form.checkedKeys)
            treeRef.value?.setCheckedKeys(res.data.data[0].checkedKeys)
            //获取权限列表并且渲染视图 
        }
    })
}
addlistone();
//点击修改管理员数据
const editOperFn = () => {
    //获取权限ID 
    const checkedKeys = (treeRef.value?.getCheckedKeys() || []);
    const halfCheckedKeys = (treeRef.value?.getHalfCheckedKeys() || []);

    form.checkedKeys = [...checkedKeys, ...halfCheckedKeys];

    editOperatorApi(form).then(res => {
        if (res.data.code == 200) {
            ElMessage({
                type: "success",
                message: "修改成功",
            });
            emits("onCleas"); //触发父组件事件关闭窗口
        }
    })
}

//监听父组件传来的点击事件的值，如果点击修改，则发请求获取管理员数据，更新视图列表
//不监听则，传值第一个，不会更新点击事件，点谁都是一次点击的数据
watch(props, () => {
    addlistone();
})
//点击取消
const cancelFn = () => {
    emits("onCleas")
}




</script>
<style lang="scss" scoped></style>
